package com.sgb.kjwl.view.ui.adapter.self.logistics;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.OrderQuoteEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.activity.transport.BiddingOrderDetailsActivity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.im.immodel.IMUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

public class OrderOwnerAdapter extends BaseRecycleAdapter<OrderQuoteEntity> {

    private onEditClickListener onEditClickListener;

    public OrderOwnerAdapter(Context context, List<OrderQuoteEntity> data) {
        super(context, R.layout.item_order_owner, data);
    }

    public void setonEditClick(onEditClickListener editClickListener) {
        this.onEditClickListener = editClickListener;
    }

    public interface onEditClickListener {
        void onAgreeClick(OrderQuoteEntity entity);
    }

    @Override
    public void convert(ItemViewHolder holder, OrderQuoteEntity entity) {
        //设置绑定数据
        holder.setBinding(BR.viewModel, entity);
        ImageView status_iv = holder.getView(R.id.status_iv);
        holder.itemView.findViewById(R.id.handle_btn).setVisibility(View.GONE);
        holder.itemView.findViewById(R.id.handle_bar).setVisibility(View.GONE);
        holder.itemView.findViewById(R.id.view_order).setVisibility(View.GONE);

        //报价状态：2.0.8  报价状态：offerStatus     1已报价  2已同意  5货主未同意
        switch (entity.getOfferStatus()) {
            case "1"://已报价
                status_iv.setImageResource(R.mipmap.yijiedan);
                holder.itemView.findViewById(R.id.handle_bar).setVisibility(View.VISIBLE);
                holder.itemView.findViewById(R.id.handle_btn).setVisibility(View.VISIBLE);
                holder.itemView.findViewById(R.id.view_order).setVisibility(View.GONE);
                break;

            case "2"://已同意
                status_iv.setImageResource(R.drawable.agree);
                holder.itemView.findViewById(R.id.handle_bar).setVisibility(View.VISIBLE);
                holder.itemView.findViewById(R.id.handle_btn).setVisibility(View.GONE);
                holder.itemView.findViewById(R.id.view_order).setVisibility(View.VISIBLE);
                break;

            case "5"://未同意
                status_iv.setImageResource(R.mipmap.diagree_order);
                holder.itemView.findViewById(R.id.handle_bar).setVisibility(View.VISIBLE);
                holder.itemView.findViewById(R.id.handle_btn).setVisibility(View.GONE);
                holder.itemView.findViewById(R.id.view_order).setVisibility(View.GONE);
                break;
        }

        TextView tv_name = holder.getView(R.id.tv_name);
        tv_name.setText(TextUtils.isEmpty(entity.getName()) ? "" : entity.getName());
        TextView tv_phone = holder.getView(R.id.tv_phone);
        tv_phone.setText(TextUtils.isEmpty(entity.getTelphone()) ? "" : entity.getTelphone());

        TextView freight_offer = holder.itemView.findViewById(R.id.freight_offer);
        TextView car_info = holder.itemView.findViewById(R.id.car_info);
        LinearLayout car_info_layout = holder.itemView.findViewById(R.id.car_info_layout);
        TextView offer_time = holder.itemView.findViewById(R.id.offer_time);
        TextView title = holder.getView(R.id.name);

        if (!TextUtils.isEmpty(entity.getQuotedPriceObject())) {
            switch (entity.getQuotedPriceObject()) {
                case "1"://企业
                case "2"://团队
                    title.setText(TextUtils.isEmpty(entity.getCompanyName()) ? "" : entity.getCompanyName() + " ");
                    car_info_layout.setVisibility(View.GONE);
                    break;
                case "3"://个人
                    title.setText(TextUtils.isEmpty(entity.getCarNumber()) ? "" : entity.getCarNumber());
                    String carInfo = "";
                    if (!TextUtils.isEmpty(entity.getCarTypeName()))
                        carInfo += entity.getCarTypeName() + " ";
                    if (!TextUtils.isEmpty(entity.getCarLenName()))
                        carInfo += "车长" + entity.getCarLenName();
                    car_info.setText(TextUtils.isEmpty(carInfo) ? "/" : carInfo);
                    car_info_layout.setVisibility(View.VISIBLE);
                    break;
            }
        }

        if (!TextUtils.isEmpty(entity.getFreight())) {
            freight_offer.setText(NumberUtil.trim0(entity.getFreight()) + "元/吨");
            if (!TextUtils.isEmpty(entity.getSourceAllFreight()) && !TextUtils.isEmpty(entity.getAllFreight())) {
                freight_offer.setTextColor(Double.parseDouble(entity.getAllFreight()) > Double.parseDouble(entity.getSourceAllFreight())
                        ? context.getResources().getColor(R.color.color_FF4B10) : context.getResources().getColor(R.color.color_secondary_yellow));
            }
        } else {
            freight_offer.setText("/");
        }

        if (!TextUtils.isEmpty(entity.getCreateTime())) {
            offer_time.setText(DateUtils.formatDate(String.valueOf(entity.getCreateTime()), ""));
        } else {
            offer_time.setText("/");
        }

        holder.itemView.findViewById(R.id.handle_btn).setOnClickListener(v -> checkCarInfo(entity));
        holder.itemView.findViewById(R.id.view_order).setOnClickListener(v -> skipOrder(entity));
        holder.itemView.findViewById(R.id.tv_telephone).setOnClickListener(v -> checkCanCall(entity));
        holder.itemView.findViewById(R.id.tv_chat).setOnClickListener(v -> checkIm(entity));

    }

    /**
     * 跳转竞价订单详情
     *
     * @param entity
     */
    private void skipOrder(OrderQuoteEntity entity) {
        if (!TextUtils.isEmpty(entity.getOrderNo())) {
            //跳转竞价订单详情
            BiddingOrderDetailsActivity.start(context, entity.getOrderNo(), 0);
        }
    }

    /**
     * 拨号检查
     *
     * @param entity
     */
    private void checkCanCall(OrderQuoteEntity entity) {
        if (!isLogin()) {
            VerificationCodeLoginActivity.start(context);
            return;
        }
        if (!TextUtils.isEmpty(entity.getTelphone())) {
            showCallDialog(entity.getTelphone());
        } else {
            ToastUtil.show(context, "无联系方式");
        }
    }

    /**
     * 交流检查
     *
     * @param entity
     */
    private void checkIm(OrderQuoteEntity entity) {
        if (entity != null && !TextUtils.isEmpty(entity.getImAccount())) {
//            MToast.showToast(context, "功能暂未实现");
//            NimUIKit.startP2PSession(context, entity.getImAccount());
            IMUtils.getInstance().buildSingleTalking(entity.getImAccount());
        }
    }

    private boolean isLogin() {
        return !TextUtils.isEmpty(SharedPreferenceUtil.getInstance().
                getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""));
    }

    /**
     * 检查车辆信息是否正常
     *
     * @param entity
     */
    private void checkCarInfo(OrderQuoteEntity entity) {
        showOfferDialog(entity);
    }

    /**
     * 拨打电话对话框
     *
     * @param number
     */
    private void showCallDialog(String number) {
        new RoundDialog.Builder(context)
                .title("确认呼叫")
                .content(number)
                .cancel("取消")
                .confirm("确定")
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .confirmCallback(new RoundDialog.SingleCallback() {
                    @Override
                    public void onClick(View v) {
                        callPhone(number);
                    }
                })
                .show();
    }

    /**
     * 展示同意报价对话框
     */
    private void showOfferDialog(OrderQuoteEntity entity) {
        String name = "";
        switch (entity.getQuotedPriceObject()) {
            case "1"://企业
            case "2"://团队
                name = entity.getCompanyName();
                break;
            case "3"://个人
                name = entity.getName();
                break;
        }
        new RoundDialog.Builder(context)
                .title("提示")
                .content("您确定要同意" + name + "的接单吗？确定后将生成竞价订单")
                .cancel("取消")
                .confirm("确定")
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .confirmCallback(v -> {
                    if (onEditClickListener != null) {
                        onEditClickListener.onAgreeClick(entity);
                    }
                })
                .show();
    }

    protected void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        context.startActivity(intent);
    }
}
